package com.happy.service.impl;

import com.happy.dao.RequestMessageDao;
import com.happy.pojo.RequestMessage;
import com.happy.pojo.User;
import com.happy.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: happy
 * @Date: 2021/9/21 21:47
 * @Version: 1.0
 */
@Transactional
@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private RequestMessageDao requestMessageDao;

    /**
     * 根据用户查询请求信息
     *
     * @param user
     * @return
     */
    @Override
    public List<RequestMessage> findModelsByUser(User user) {
        List<RequestMessage> resultList = new ArrayList<>();
        try {
            List<RequestMessage> myRequestMessageList = new ArrayList<>();
            if (user != null) {
                // 获取该用户未公开的请求消息列表
                myRequestMessageList = requestMessageDao.selectByUserIdNotPublic(user.getId());
            }
            // 获取所有公开的请求消息列表
            List<RequestMessage> publicRequestMessageList = requestMessageDao.selectByIsPublic("1");

            // 使用 addAll 方法将用户未公开的请求消息列表和公开的请求消息列表合并到 resultList 中
            boolean flag1 = resultList.addAll(myRequestMessageList);
            boolean flag2 = resultList.addAll(publicRequestMessageList);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 返回resultList
        return resultList;
    }
}
